import time

from 水质.utils.Logger import logger
from 水质.utils.click import clickText, clickId, swipeUp, swipeDown
from appium.webdriver.common.appiumby import AppiumBy

from 水质.流量监测.流量报表 import checkMaxMinTimeExsit


def checkDayMonth(driver):
    parent = driver.find_element(by=AppiumBy.ID, value="title_container")
    els = parent.find_elements(by=AppiumBy.CLASS_NAME, value="android.view.View")
    if len(els) > 1:
        els[1].click()
        logger.log("点击月元素")

    else:
        logger.log("没有月元素切换")


def checkDay(driver):
    parent = driver.find_element(by=AppiumBy.ID, value="title_container")
    els = parent.find_elements(by=AppiumBy.CLASS_NAME, value="android.view.View")
    if len(els) > 0:
        els[0].click()
        logger.log("点击日元素")

    else:
        logger.log("没有日元素切换")


def checkDatePick(driver):
    clickId(driver, "textView406")
    clickText(driver, "今天")
    clickText(driver, "确认")

    clickId(driver, "textView406")
    clickText(driver, "昨天")
    clickText(driver, "确认")
    clickId(driver, "textView406")
    parent = driver.find_element(by=AppiumBy.ID, value="datePickerView")
    els = parent.find_elements(by=AppiumBy.CLASS_NAME, value="android.view.View")
    if len(els) > 0:
        year = els[0]
        swipeUp(driver, year)
    if len(els) > 1:
        year = els[1]
        swipeDown(driver, year)
    if len(els) > 2:
        year = els[2]
        swipeUp(driver, year)

    clickText(driver, "确认")


def checkTableWater(driver):
    clickId(driver, "iv_menu")
    clickText(driver, "水位报表")

    # 先切换到月
    checkDayMonth(driver)
    checkMaxMinTimeExsit(driver)
    checkDatePick(driver)
    clickText(driver, "雷达流速仪")
    checkMaxMinTimeExsit(driver)

    clickText(driver, "在线ADCP")
    checkMaxMinTimeExsit(driver)

    clickText(driver, "雷达水位计")
    checkMaxMinTimeExsit(driver)

    checkDay(driver)
    checkDatePick(driver)
    checkMaxMinTimeExsit(driver)

    clickText(driver, "雷达流速仪")
    clickText(driver, "在线ADCP")
    checkMaxMinTimeExsit(driver)

    clickText(driver, "雷达水位计")
